
// 正则表达式验证类
class ValidatorFactory {
    constructor() {
        return function (name) {
            return new ValidatorBase(name)
        }
    }
}

class ValidatorBase {
    constructor(name) {
        this.validatorName = name;
    }
    getPattern(name) {
        try {
            return patterns[name]
        } catch (error) {
            throw new Error('啊,这~ 没有这个方法!') //吾辈当自强!
        }
    }
    assert(str) {
        let reg = this.getPattern(this.validatorName);
        return reg.test(str)
    }
}


let patterns = {
    phone: /^[1][3,4,5,6,7,8,9][0-9]{9}$/,
    idCard: /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
};

export let Validator = new ValidatorFactory()